<template>
  <div class="login-body">
    <div class="main">
      <div class="left">
        <img src="@/assets/login.png" style="width: 100%; height: 100%; border-radius: 10px;">
      </div>
      <div class="right">
        <div div class="login-font">
          登录
        </div>
        <div style="width: 100%;">
          <el-form :model="user" :rules="rules" ref="ruleForm" style="padding: 20px;">
            <el-form-item prop="username">
              <el-input size="medium" clearable v-model="user.username" placeholder="请输入账号">
                <template #prefix>
                  <i class="el-icon-user" style="font-size: 18px;padding-top: 8px;"></i>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input size="medium" clearable show-password v-model="user.password" placeholder="请输入密码">
                <template #prefix>
                  <i class="el-icon-lock" style="font-size: 18px;padding-top: 8px;"></i>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <div style="display: flex;">
                <el-input size="medium" v-model="inputVal" clearable placeholder="验证码">
                  <template #prefix>
                    <i class="el-icon-yanzhengma" style="font-size: 18px;padding-top: 8px;"></i>
                  </template></el-input>
                <div style="margin-left: 10px;">
                  <ValidCode ref="ref_validateCode" @change="changeCode"></ValidCode>
                </div>
              </div>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="medium" style="width: 100%;" @click="login" autofocus>登录</el-button>
              <span>
                <el-checkbox label="记住我" v-model="checked"></el-checkbox>
              </span>
              <div style="display: flex;">
                还没账号？ <div style="flex:1;cursor: pointer;color: #409eff;" @click="$router.push('/register')">注册</div>
                <div style="text-align: right;cursor: pointer;flex:1;color: #409eff;" @click="dialogFormVisible = true">
                  重置密码</div>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="dialog-container">
        <el-dialog :visible.sync="dialogFormVisible" @close="closeDialog">
          <el-form :model="form" ref="form2" :rules="rules2">
            <el-form-item prop="username">
              <el-input size="medium" v-model="form.username" placeholder="输入账号"></el-input>
            </el-form-item>
            <el-form-item prop="email">
              <EmailVal v-model="form.email"></EmailVal>
            </el-form-item>
            <el-form-item prop="val">
              <el-input size="medium" v-model="form.val" placeholder="输入验证码"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button size="small" @click="closeDialog">取 消</el-button>
            <el-button size="small" type="primary" @click="ForgetPassowrd">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import ValidCode from '@/components/ValidCode'
import EmailVal from '@/components/EmailVal.vue'
import { loginAPI,reSetPasswordAPI } from '@/api/web'
export default {
  components: {
    ValidCode,
    EmailVal
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        username: '',
        email: '',
        val: ''
      },
      checked: true,
      user: {
        username: '',//账号
        password: ''//密码
      },
      inputVal: "",//输入的验证码
      checkCode: "",//验证码传进来的数据
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
      },
      rules2: {
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
        ],
        val: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
        ]
      }
    }
  },
  mounted() {
    this.userLocal()
  },
  methods: {
    closeDialog() {
      this.form.username = ''
      this.form.email = '',
        this.form.val = '',
        this.dialogFormVisible = false
      this.$refs.form2.clearValidate();
    },
    ForgetPassowrd() {
      this.$refs.form2.validate((valid) => {
        if (valid) {
          this.$confirm("你确定要重置吗？").then(async (ok) => {
            if (ok) {
              const res = await reSetPasswordAPI(this.form)
              if (res.code == '200') {
                this.$message.success("密码已重置为：123456")
                this.closeDialog()
              } else {
                this.$message.error(res.msg)
              }
            }
          })
        }
      })
    },
    changeCode(value) {
      this.checkCode = value//获取传进来的验证码
    },
    // 如果本地有账号密码就自动填充
    userLocal() {
      if (localStorage.getItem("login")) {
        this.user = JSON.parse(localStorage.getItem("login"))
      }
    },
    //登录
    login() {
      this.$refs.ruleForm.validate(async (ok) => {
        if (ok) {
          if (this.checkCode === this.inputVal.toUpperCase()) {//不区分大小写验证(全部转换为大写)
            const res = await loginAPI(this.user)
            if (res.code == 200) {
              this.$message.success("登录成功")

              localStorage.setItem("user", JSON.stringify(res.data.rows))//存储用户信息
              localStorage.setItem("token", JSON.stringify(res.data.token))//存储用户信息
              //记住密码
              if (this.checked) {
                localStorage.setItem("login", JSON.stringify(this.user))
              } else {
                localStorage.removeItem("login")
              }
              this.$router.push('/')
            } else {
              this.$message.error(res.msg)
            }
          } else {
            this.$message.error("验证码错误")
            this.inputVal = "";
          }
          this.$refs["ref_validateCode"].draw();
        }
      })

    }
  }
}
</script>

<style>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 97vh;
  /* width: 100%; */
  background-image: url('@/assets/login-background.png');
  background-size: cover;
  background-position: center;

  .right {
    margin-left: 10px;
    width: 300px;
  }

  .main {
    width: 550px;
    border-radius: 10px;
    margin-left: 500px;
    /* 设置左边距 */
    height: 70vh;
    display: flex;
    background-color: white;

    .dialog-container {
      .el-dialog {
        width: 30% !important;
      }
    }

    .left {
      width: 50%;
    }
  }

  .login-font {
    font-weight: bold;
    font-size: 30px;
    padding-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #409eff;
  }

  .el-form-item {
    width: 100%;
  }
}
</style>